#include "..\..\..\LunarLib\include\LunarWin32.h"
#include "..\..\..\LunarLib\include\L3DEngine.h"
#include "d3d.h"
#include "MyCamera.h"

#pragma comment (lib, "LunarWin32.lib")

MyWin32*		pMain = NULL;
MyD3D*			pMyd3d = NULL;
MyCamera*		pMyCamera = NULL;

MSG			msg;

void KeyDown();

int Processmsg()
{
	 while( msg.message != WM_QUIT )
	 {
		 if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
		 {
			 TranslateMessage( &msg );
			 DispatchMessage( &msg );
		 }
		else
		{
			pMyd3d->Render();
			KeyDown();
		 }
	}
	return msg.wParam;
}


void KeyDown()
{
	if (GetAsyncKeyState(VK_F1))
	{
		pMyd3d->SetTextureAddress(1);
	}
	if (GetAsyncKeyState(VK_F2))
	{
		pMyd3d->SetTextureAddress(2);
	}
	if (GetAsyncKeyState(VK_F3))
	{
		pMyd3d->SetTextureAddress(3);
	}
	if (GetAsyncKeyState(VK_F4))
	{
		pMyd3d->SetTextureAddress(4);
	}

	if (GetAsyncKeyState(0x57))   // when press 'w' key...
	{
		pMyd3d->GoFoward();
	}

	if (GetAsyncKeyState(0x53))    // when press 's' key...
	{
		pMyd3d->GoBackward();
	}

	if (GetAsyncKeyState(VK_RIGHT))
	{
		//pMyd3d->RotateCCW();
		pMyCamera->RotationY(0.01f);
	}

	if (GetAsyncKeyState(VK_LEFT))
	{
		pMyd3d->RotateCW();
	}

	if (GetAsyncKeyState(VK_UP))
	{
		pMyd3d->CameraUp();
	}

	if (GetAsyncKeyState(VK_DOWN))
	{
		pMyd3d->CameraDown();

	}
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	HWND  hWnd;

	pMain = new MyWin32();
	pMyd3d = new MyD3D();
	pMyCamera = new MyCamera();

	pMain->Create(1, 800, 600, "Direct3D9 Example 5th - Texture");
	hWnd = pMain->GetMyWinHandle();
	
	if (!hWnd )
		return E_FAIL;
	if (FAILED (pMyd3d->InitD3D( hWnd ) ) )
		return E_FAIL;
	if (FAILED (pMyd3d->InitVB() ) )
		return E_FAIL;
	
	Processmsg();
	
	delete pMyCamera;
	delete pMyd3d;
	delete pMain;
}

